<?php

/**
 * Class Lib_Repair_Main
 * 失联修复主类
 * 2020/11/10
 */
class Lib_Repair_Main
{

    /**
     * 失联修复接口
     * @param $data   发送应用
     * @return array
     * @throws Exception
     */
    public static function repair($data)
    {
        try {
            //使用修复通道
            $use_pass = Lib_Repair_Config::getConfig('use_pass',0);
            if($use_pass == 'ZaiRui'){
                //目前用再芮整合接口
                $pass = Lib_Repair_Adapter::getAdapter('ZaiRui');
                return $pass->repair($data['data']);
            }else{
                $slave_pass = Lib_Repair_Config::getConfig('pass',0);
                if(empty($data['pass']) || !in_array($data['pass'],$slave_pass)){
                    Func_Log::write('repair',   'main',$data['pass'].'loss repair --'.' responsedata: '. json_encode($data). json_encode($slave_pass));
                    return false;
                }
                $pass = Lib_Repair_Adapter::getAdapter($data['pass']);
                return $pass->repair($data['data']);
            }

        } catch (Exception $exception) {
            Func_Log::write('Exception',   $exception->getMessage(), 'responsedata : '. json_encode($data));
        }
        return ['code'=>500,'message' => $exception->getMessage() ,'data'=>[]];

    }
    /**
     * 绑定呼叫号码
     * @param $data   发送应用
     * @return array
     * @throws Exception
     */
    public static function bindTel($data)
    {
        try {
            //使用修复通道
            $use_pass = Lib_Repair_Config::getConfig('use_pass',0);
            if($use_pass == 'ZaiRui'){
                $pass = Lib_Repair_Adapter::getAdapter('ZaiRui');
                return $pass->bindTel($data['data']);
            }else{
                $slave_pass = Lib_Repair_Config::getConfig('pass',0);
                if(empty($data['pass']) || !in_array($data['pass'],$slave_pass)){
                    Func_Log::write('repair',   'main',$data['pass'].'loss repair --'.' responsedata: '. json_encode($data). json_encode($slave_pass));
                    return false;
                }
                $pass = Lib_Repair_Adapter::getAdapter($data['pass']);
                return $pass->bindTel($data['data']);
            }
        } catch (Exception $exception) {
            Func_Log::write('Exception',   $exception->getMessage(), 'responsedata : '. json_encode($data));
        }
        return ['code'=>500,'message' => $exception->getMessage() ,'data'=>[]];

    }
    /**
     * 发送短信
     * @param $data   发送应用
     * @return array
     * @throws Exception
     */
    public static function sendSms($data)
    {
        try {
//            //失联修复通道
//            $slave_pass = Lib_Repair_Config::getConfig('pass',0);
//            foreach ($slave_pass as $pass) {
//                if($pass == 'YiDong'){
//                    continue;
//                }
//                if (empty($pass)) {
//                    continue;
//                }
//                $pass = Lib_Repair_Adapter::getAdapter($pass);
//                $pass->sendSms($data);
//            }
            return true;
        } catch (Exception $exception) {
            Func_Log::write('Exception',   $exception->getMessage(), 'responsedata : '. json_encode($data));
        }
        return ['code'=>500,'message' => $exception->getMessage() ,'data'=>[]];


    }
    /**
     * 解绑呼叫号码
     * @param $data
     * @return array
     * @throws Exception
     */
    public static function unBindTel($data)
    {
        try {

            $bind = Lib_Redis_String::get("bind_".$data['telX']);
            if(empty($bind)){
                return ['code'=>200,'message' => 'success' ,'data'=>[]];
            }
            $bind = json_decode($bind,true);
            $use_pass = Lib_Repair_Config::getConfig('use_pass',0);
            if($use_pass == 'ZaiRui'){
                $pass = Lib_Repair_Adapter::getAdapter('ZaiRui');
            }else{
                $pass = Lib_Repair_Adapter::getAdapter($bind['pass']);
            }
            $bind['telX'] = $data['telX'];
            return $pass->unbinding($bind);

        } catch (Exception $exception) {
            Func_Log::write('Exception',   $exception->getMessage(), 'responsedata : '. json_encode($data));
        }
        return ['code'=>500,'message' => "server error " ,'data'=>[]];

    }

}